package com.sinotrust.classify.algorithm;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.kernelab.basis.Tools;

import svm.svm_predict;

import com.sinotrust.classify.DefaultAlgorithm;
import com.sinotrust.classify.mba.DataOperation;


public class SVMAlgorithm implements DefaultAlgorithm{
	@Override
	public void predict(String index,String model,Double thredhold) {
		StringBuffer buffer = new StringBuffer();
		svm_predict p = new svm_predict();
		buffer.append("-b 1").append(" ");
		buffer.append(index).append(" ");
		buffer.append(model).append(" ");
		buffer.append("output_file").append(" ");
		String[] pargvs = buffer.toString().split(" ");
		try {
			p.predict(pargvs);
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			resultOutput("output_file",thredhold);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		File tmp = new File("output_file");
//		tmp.delete();
	}
	private void resultOutput(String output, Double thredhold) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(output));
		String tmp;
		String[] tmp1;
		int i = 0;
		while ((tmp = br.readLine()) != null) {
			if (i > 0) {
				tmp1 = tmp.split(" ");
				if (Double.parseDouble(tmp1[1]) > thredhold) {
					Tools.debug("id" + "\t" + i + "\t" + "不是一个商业网站");
				}
				if (Double.parseDouble(tmp1[1]) < thredhold) {
					Tools.debug("id" + "\t" + i + "\t" + "是一个商业网站");
				}
			}
			i++;
		}
		br.close();
	}	
@Override
	public void predict(IndexReader index, String model) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		 SVMAlgorithm s = new SVMAlgorithm();
//		 Tools.debug(s.train("indexCommercial", "indexnonindexCommercial"));
		 s.predict("dataSvmTest.txt","model_file", 0.5);

	}
	

}
